package com.moli.iov.protocol.model.realtime;

/*
 * 文件名： BmsTemperatureDataReq.java
 * 
 * 创建日期： 2016年12月14日
 *
 * Copyright(C) 2016, by <a href="mailto:liws@xingyuanauto.com">liws</a>.
 *
 * 原始作者: liws
 *
 */


import java.io.Serializable;
import java.util.Arrays;

import com.moli.iov.protocol.codec.Serialize;
import com.moli.iov.protocol.model.EntityBytesLen;
import com.moli.iov.protocol.util.UnsignedNumberUtil;
import com.moli.iov.util.ByteArrayUtil;


/**
 * 电池单体温度数据
 *
 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
 *
 * @version $Revision$
 *
 * @since 2016年12月14日
 */
@Serialize(unit="byte",isBigEndian=true)
public class BmsTemperatureInfo implements Serializable,EntityBytesLen{

	 /** 
	 */ 
	private static final long serialVersionUID = 1L;
	
	private int bytesLen = 3;
	/**
	 *
	 */
	@Override
	public int getBytesLen() {
		return bytesLen;
	}

	/**
	 * 可充电储能子系统号
	 * 
	 * 有效值范围：0～250
	 */
	@Serialize(unit="byte",offset=0,size=1,min=0,max=250)
	public int storageSubSystemCode;
	
	/**
	 * 可充电储能子系统探针个数
	 * N个探针数，有效值范围：1～65531，"0xFE"表示异常，"0xFF"表示无效。
	 * 
	 */
	@Serialize(unit="byte",offset=1,size=2,min=0,max=65531)
	public int storageSubSystemProbeSum;

	/**
	 * 可充电储能子系统各温度探针探测到的温度值
	 */
	
	public short[] probeTemperature;

	public short[] getProbeTemperature() {
		return probeTemperature;
	}
	public String getProbeTemperatureToString(){
		StringBuilder sBuilder = new StringBuilder();
		for(short s : probeTemperature){
			sBuilder.append(s+",");
		}
		return sBuilder.toString().indexOf(",")>=0?sBuilder.toString().substring(0,sBuilder.length()-1):"";
	}
	@Serialize(unit="byte",offset=3,size=-1,valOffset=40,min=1,max=250)
	public void setProbeTemperatureBytes(byte[] probeTemperature) {
		// 获取sysCode偏移量和长度
		int currentOffset = 0;
		int currentSize = this.storageSubSystemProbeSum * 1;
		byte[] currnetBytes = new byte[currentSize];
		short [] currnetShorts = new short[currentSize];
		System.arraycopy(probeTemperature, currentOffset, currnetBytes, 0, currentSize);
		short tTemperature;
		for (int i = 0; i < currnetBytes.length; i++) {
			tTemperature = UnsignedNumberUtil.toUnsignedByteValue(currnetBytes[i]);
			currnetShorts[i] = (short)(tTemperature - 40);
		}
		this.probeTemperature = currnetShorts;
		bytesLen+=currentSize;
	}
	
	@Serialize(unit="byte",offset=3,size=-1)
	public byte[] getProbeTemperatureBytes() {
		int currentSize = this.storageSubSystemProbeSum * 1;
		byte[] currnetBytes = new byte[currentSize];
		if(probeTemperature!=null&&probeTemperature.length>0){
			int i = 0;
			for(short d:probeTemperature){
				byte[] bytes = ByteArrayUtil.toBytes((short)(d+40));
				currnetBytes[i] = bytes[1];
				i++;
			}
		}
		return currnetBytes;
	}

	 
    /**
     * @return the storageSubSystemCode
     */
    public int getStorageSubSystemCode() {
        return storageSubSystemCode;
    }
    
    /**
     * @param storageSubSystemCode the storageSubSystemCode to set
     */
    public void setStorageSubSystemCode(int storageSubSystemCode) {
        this.storageSubSystemCode = storageSubSystemCode;
    }
    
    /**
     * @return the storageSubSystemProbeSum
     */
    public int getStorageSubSystemProbeSum() {
        return storageSubSystemProbeSum;
    }
    
    /**
     * @param storageSubSystemProbeSum the storageSubSystemProbeSum to set
     */
    public void setStorageSubSystemProbeSum(int storageSubSystemProbeSum) {
        this.storageSubSystemProbeSum = storageSubSystemProbeSum;
    }
    /**
	 *
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("BmsTemperatureInfo [bytesLen=");
		builder.append(bytesLen);
		builder.append(", storageSubSystemCode=");
		builder.append(storageSubSystemCode);
		builder.append(", storageSubSystemProbeSum=");
		builder.append(storageSubSystemProbeSum);
		builder.append(", probeTemperature=");
		builder.append(Arrays.toString(probeTemperature));
		builder.append("]");
		return builder.toString();
	}
}
